package cn.java63.biz2;

import cn.java63.biz.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

@Service
@Aspect
public class UserServiceLoggerAno {
    @Around("within(cn.java63.biz2..*)")
    public Object lanjie(ProceedingJoinPoint jp) throws Throwable {
        long start=System.nanoTime();
        System.out.println("请求的方法是："+jp.getSignature().getName());
        System.out.println("调用时间是："+new Date());
        System.out.println("方法的参数："+ Arrays.toString(jp.getArgs()));
        if(jp.getSignature().getName().equals("add")){
            User user=(User)jp.getArgs()[0];
            //验证一下电话
            if(user.getTele()==null||user.getTele().length()!=11){
                //throw new Exception("电话号码格式错误");
                return false;
            }
        }
        //不允许删除id=1的用户
        Object o=jp.proceed();//放行
        System.out.println("在"+new Date()+"调用完成，返回值是："+o);
        long end=System.nanoTime();
        System.out.println(end-start);
        return o;
    }
    @Before("within(cn.java63.biz2..*)")
    public void before(JoinPoint jp){
        System.out.println(">?");
    }
}
